package com.kjsb.controller;

import com.kjsb.entity.User;
import com.kjsb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author li xiaoYu
 * @data 2022年 11月 23日13:09
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService service;

    /**
     * 登录
     * @return Result结果集
     */
    @GetMapping("/{username}/{password}")
    public Result login(@PathVariable("username") String username,@PathVariable("password") String password){
        boolean login = service.login(username,password);
        Integer code=login ? Code.GET_OK : Code.GET_ERR;
        String msg= login ? "登录成功！" : "登录失败，请重试！";
        return new Result(code,login,msg);
    }

    /**
     * 查询所有用户
     * @return Result结果集
     */
    @GetMapping
    public Result getAllUsers(){
        List<User> list = service.getAllUsers();
        Integer code=list != null ? Code.GET_OK : Code.GET_ERR;
        String msg= list != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    /**
     * 删除用户
     * @return Result结果集
     */
    @PutMapping("/{id}")
    public Result deleteUsers(@PathVariable Integer id){
        boolean i =service.deleteUsers(id);
        Integer code=i? Code.DELETE_OK : Code.DELETE_ERR;
        String msg= i? "删除成功！" : "删除失败，请重试！";
        return new Result(code,i,msg);
    }

    /**
     * 新增用户
     * @param user 用户信息
     * @return Result结果集
     */
    @PostMapping
    public Result saveUsers(@RequestBody User user){
        boolean i = service.saveUsers(user);
        Integer code=i == true ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg= i == true ? "添加成功！" : "添加失败，请重试！";
        return new Result(code,i,msg);
    }

}
